import InvocationHandler from "../../db/proxy/InvocationHandler";
import  RedisClient from "./redis/Redis";
import  LocalClient from "./local"


export default class InjectRedisHandler extends InvocationHandler {

    constructor() {
        super();
       
    }

    async invoke(proxy, method, methodName, args) {
        return await super.invoke(proxy, method, methodName, args);;
    }
    

    /**
     * 注入
     * @param {*} proxy 
     */
    inject(proxy){
        try{
            const jedis =  new RedisClient();
            Reflect.set(proxy, 'jedis', jedis);
            const local =  new LocalClient();
            Reflect.set(proxy, 'local', local);
        }catch(e){
            this.handlerError(e);
        }
        return  super.inject(proxy);
    }

    /**
     * 处理错误
     * @param e
     */
    handlerError(e) {
        switch (e.errno) {
            case 1062:    //索引重复
                throw ({message: "索引重复,请重新创建!"})
                break;
            case 0:
                break;
            default:
                throw (e);
        }


    }


}
